<template>
  <a-spin :spinning="loading">
    <a-form v-bind="formItemLayout">
      <a-row>
        <a-col :span="24">
          <a-form-item label="检查结果" v-bind="validateInfos.result">
            <a-input v-model:value="formData.result" placeholder="请输入检查结果" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label=" 检查人" v-bind="validateInfos.inspectionPersonnel">
            <a-input v-model:value="formData.inspectionPersonnel" placeholder="请输入 检查人" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="问题描述" v-bind="validateInfos.problemDescription">
            <a-input v-model:value="formData.problemDescription" placeholder="请输入问题描述" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label=" 整改期限" v-bind="validateInfos.rectificationDeadline">
		        <a-date-picker placeholder="请选择 整改期限"  v-model:value="formData.rectificationDeadline" showTime value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="整改人" v-bind="validateInfos.rectifierPerson">
            <a-input v-model:value="formData.rectifierPerson" placeholder="请输入整改人" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="复查人" v-bind="validateInfos.reviewerPerson">
            <a-input v-model:value="formData.reviewerPerson" placeholder="请输入复查人" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="问题照片" v-bind="validateInfos.problemPhoto">
	          <j-image-upload  v-model:value="formData.problemPhoto" :disabled="disabled"></j-image-upload>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label=" 结论" v-bind="validateInfos.conclusion">
            <a-input v-model:value="formData.conclusion" placeholder="请输入 结论" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="复查状态" v-bind="validateInfos.reviewerStatus">
            <j-dict-select-tag type='radio' v-model:value="formData.reviewerStatus" dictCode="zy_review_status" placeholder="请选择复查状态" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="整改状态" v-bind="validateInfos.rectifierStatus">
            <j-dict-select-tag type='radio' v-model:value="formData.rectifierStatus" dictCode="zy_rectifier_status" placeholder="请选择整改状态" :disabled="disabled"/>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>

		<!-- 子表单区域 -->
    <a-tabs v-model:activeKey="activeKey" animated>
      <a-tab-pane tab="中阳整改信息" key="zyRectificationInformation" :forceRender="true">
        <zy-rectification-information-form ref="zyRectificationInformationFormRef" :disabled="disabled"></zy-rectification-information-form>
      </a-tab-pane>
      <a-tab-pane tab="中阳复查信息" key="zyReviewInformation" :forceRender="true">
        <zy-review-information-form ref="zyReviewInformationFormRef" :disabled="disabled"></zy-review-information-form>
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script lang="ts">
  import { defineComponent, ref, reactive, computed, toRaw, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useValidateAntFormAndTable } from '/@/hooks/system/useJvxeMethods';
  import { queryDataById, saveOrUpdate } from '../ZyHazardInvestigation.api';
  import ZyRectificationInformationForm from './ZyRectificationInformationForm.vue'
  import ZyReviewInformationForm from './ZyReviewInformationForm.vue'
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import JImageUpload from '/@/components/Form/src/jeecg/components/JImageUpload.vue';
  import { Form } from 'ant-design-vue';
  const useForm = Form.useForm;

  export default defineComponent({
    name: "ZyHazardInvestigationForm",
    components:{
      JDictSelectTag,
      JImageUpload,
      ZyRectificationInformationForm,
      ZyReviewInformationForm,
    },
    props:{
      formDisabled:{
        type: Boolean,
        default: false
      },
      formData: { type: Object, default: ()=>{} },
      formBpm: { type: Boolean, default: true }
    },
    emits:['success'],
    setup(props, {emit}) {
      const loading = ref(false);
      const zyRectificationInformationFormRef = ref();
      const zyReviewInformationFormRef = ref();
      const activeKey = ref('zyRectificationInformation');
      const formData = reactive<Record<string, any>>({
        id: '',
        result: '',   
        inspectionPersonnel: '',   
        problemDescription: '',   
        rectificationDeadline: '',   
        rectifierPerson: '',   
        reviewerPerson: '',   
        problemPhoto: '',   
        conclusion: '',   
        reviewerStatus: '',   
        rectifierStatus: '',   
      });

      //表单验证
      const validatorRules = reactive({
      });
      const {resetFields, validate, validateInfos} = useForm(formData, validatorRules, {immediate: true});
      const dbData = {};
      const formItemLayout = {
        labelCol: {xs: {span: 24}, sm: {span: 5}},
        wrapperCol: {xs: {span: 24}, sm: {span: 16}},
      };

      // 表单禁用
      const disabled = computed(()=>{
        if(props.formBpm === true){
          if(props.formData.disabled === false){
            return false;
          }else{
            return true;
          }
        }
        return props.formDisabled;
      });

      

      function add() {
        resetFields();
        zyRectificationInformationFormRef.value.initFormData();
        zyReviewInformationFormRef.value.initFormData();
      }

      async function edit(row) {
        //主表数据
        await queryMainData(row.id);
        //子表数据
        await zyRectificationInformationFormRef.value.initFormData(row['id']);
        await zyReviewInformationFormRef.value.initFormData(row['id']);
      }

      async function queryMainData(id) {
        const row = await queryDataById(id);
        Object.keys(row).map(k => {
          formData[k] = row[k];
        });
      }

      const {getSubFormAndTableData, transformData} = useValidateAntFormAndTable(activeKey, {
        'zyRectificationInformation': zyRectificationInformationFormRef,
        'zyReviewInformation': zyReviewInformationFormRef,
      });

      async function getFormData() {
        await validate();
        return transformData(toRaw(formData))
      }

      async function submitForm() {
        const mainData = await getFormData();
        const subData = await getSubFormAndTableData();
        const values = Object.assign({}, dbData, mainData, subData);
        console.log('表单提交数据', values)
        const isUpdate = values.id ? true : false
        await saveOrUpdate(values, isUpdate);
        //关闭弹窗
        emit('success');
      }
      
      function setFieldsValue(values) {
        if(values){
          Object.keys(values).map(k=>{
            formData[k] = values[k];
          });
        }
      }

      /**
       * 值改变事件触发-树控件回调
       * @param key
       * @param value
       */
      function handleFormChange(key, value) {
        formData[key] = value;
      }


      return {
        zyRectificationInformationFormRef,
        zyReviewInformationFormRef,
        validatorRules,
        validateInfos,
        activeKey,
        loading,
        formData,
        setFieldsValue,
        handleFormChange,
        formItemLayout,
        disabled,
        getFormData,
        submitForm,
        add,
        edit
      }
    }
  });
</script>